<?php

/*
 * ************************************************* 
 * Created on :2012-5-5 11:58:24
 * Encoding   :UTF-8
 * Description:
 * 
 * @Author @lqs <soso2501@gmail.com>
 * @WAP_WEIBO (C)1996-2099 SINA Inc. 
 * ************************************************
 */

class Toolfunc {

    /**
     * 将传过来的新闻数组里加一个链接
     * 改变原来数组
     * @param type $news 
     */
    public static function addnewslink(&$news) {
        foreach ($news as $k => $v) {
            $news[$k]['link'] = self::getnewslink($v['newsid'], $v['newstype']);
        }
    }

    /**
     * 获取指定新闻id与新闻类型的链接
     * @param type $newsid
     * @param type $newstype 
     */
    public static function getnewslink($newsid, $newstype) {
        $c = '';
        switch ($newstype) {
            case "河工新闻": $a = 'hgxw';
                $c = 'xinwenkuaixun';
                break;
            case "校友会快讯":$a = 'xyhkx';
                $c = 'xinwenkuaixun';
                break;
            case "人物动态": $a = 'rwdt';
                $c = 'xinwenkuaixun';
                break;
            case "领导关怀": $a = 'ldgh';
                $c = 'xinwenkuaixun';
                break;
            case "捐赠事迹":$a = 'sj';
                $c = 'juanzeng';
                break;
            case "年度捐赠":$a = 'nd';
                $c = 'juanzeng';
                break;
            case "校庆精选文稿汇编": $a = 'hb';
                $c = 'bainianxiaoqing';
                break;
            case "分会新闻":$a = 'fhxw';
                $c = 'zuzhi';
                break;
            case "校友通讯":$a = 'xytx';
                $c = 'fengcai';
                break;
            case "最新服务":$a = 'zxfw';
                $c = 'fuwu';
                break;
            case "讲堂动态":$a = 'jtdt';
                $c = 'jiangtang';
                break;
        }
        if (empty($c))
            return "/";
        else
            return "/$c/$a?id=$newsid";
    }

    public static function paging($n, $page_size = PAGE_SIZE) {
        $page = empty($_REQUEST['page']) ? 1 : intval($_REQUEST['page']);
        if ($page <= 0)
            $page = 1;
        $offset = ($page - 1) * $page_size;
        $url = $_SERVER['REQUEST_URI'];
        if (preg_match("/[\?&]?page=[0-9]+/", $url, $matches)) {
            $url = str_replace($matches[0], '', $url);
        }
        $url = strpos($url, '?') ? $url . '&' : $url . '?';
        $result = '';
        $page_count = ceil($n / $page_size); //一共有多少页
        if ($page_size < $n) {
            if ($page > 1) {
                $pre = $page - 1;
                $result .= "<a href=" . $url . "page=" . $pre . ">&lt;&lt</a>";
            } else {
                $result .= "<a>&lt;&lt;</a>";
            }
            if ($page_count <= PAGE_SHOW) {
                $start = 1;
                $end = $page_count;
            } else {
                $middle = intval(PAGE_SHOW / 2);
                if ($page > $middle) {
                    $end = $page + $middle;
                    if ($end > $page_count) {
                        $end = $page_count;
                        $start = $end - PAGE_SHOW + 1;
                    } else {
                        $start = $page - $middle;
                    }
                } else {
                    $start = 1;
                    $end = ($page_count < PAGE_SHOW) ? $page_count : PAGE_SHOW;
                }
            }
            for ($i = $start; $i <= $end; $i++) {
                if ($i == $page) {
                    $result .= "<strong>" . $i . "</strong>";
                } else {
                    $result .= "<a href=" . $url . "page=" . $i . ">" . $i . "</a>";
                }
            }
            if ($page < $end) {
                $next = $page + 1;
                $result .= "<a href=" . $url . "page=" . $next . ">&gt;&gt;</a>";
            } else {
                $result .= "<a>&gt;&gt;</a>";
            }
            $result .= '
            <label>
                <input type="text" title="输入页码，按回车快速跳转" class="pagejump" data-url="' . $url . '"/>
                <span title="共 ' . $page_count . ' 页"> / ' . $page_count . ' 页</span>
            </label>';
        }
        return array($result, $offset);
    }

    public static function shortstr($str, $length) {
        $r = mb_substr($str, 0, $length, "utf8");
        if (mb_strlen($str, "utf8") > $length)
            $r.= "...";
        return $r;
    }

}

/* End of file Toolfunc */


